<template>
  <div class="mainPageClass" style="height: 100%;background: #fff;">
   
  </div>
</template>
<script lang="ts">
import 'vant/lib/toast/style';
import { Toast, Dialog } from 'vant';
import { cloudApi } from '@/api/indexApi';
import { Component, Vue, Prop } from 'vue-property-decorator';
import Header from '@/components/Header.vue';
Component.registerHooks(['beforeRouteUpdate', 'beforeRouteLeave', 'beforeRouteLeave']);
@Component({
  components: { [Dialog.Component.name]: Dialog.Component,Header },
})
export default class MainPage extends Vue {
  openId:any=''
  mobile:any=''
  urlPro:any=''
  beforeCreate(){
    if(window.location.href.indexOf("_Myinsure")!=-1&&window.location.href.indexOf("#/Myinsure")==-1){
      window.location.href=window.location.href+'Myinsure'
    }
    if(window.location.href.indexOf("_mainPage")!=-1&&window.location.href.indexOf("#/mainPage")==-1){
      window.location.href=window.location.href+'mainPage'
    }
    if(window.location.href.indexOf("_checkPhoneInfo")!=-1&&window.location.href.indexOf("#/checkPhoneInfo")==-1){
        window.location.href=window.location.href+'checkPhoneInfo'
    }
    if(window.location.href.indexOf("_personalCard")!=-1&&window.location.href.indexOf("#/personalCard")==-1){
      window.location.href=window.location.href+'personalCard'
    }
    if(window.location.href.indexOf("pwebview")!=-1&&window.location.href.indexOf("#/pwebview")==-1){//产品授权
      var _this = this;
      var address = location.href;
      let appid = address.split("appid=")[1].split("&code")[0];
      let code = address.split("code=")[1].split("&state")[0]
      cloudApi.showInfo({app_id: appid, code: code}).then(function(res: any) {
          if (res.data) {
            if(res.data.openId){
              _this.openId=res.data.openId
              console.log("zwhurlOpenId",_this.openId)
            _this.getUserData()
            }
          }
        });
      }
    if(window.location.href.indexOf("_ExternalLinks")!=-1&&window.location.href.indexOf("#/ExternalLinks")==-1){//公众号跳转外部链接先授权
      var _this = this;
      var address = location.href;
      let appid = address.split("appid=")[1].split("&code")[0];
      let code = address.split("code=")[1].split("&state")[0]
      cloudApi.getUnionIdAndOpenId({appid:appid, code:code}).then(function(res: any) {
        if (res.data) {
          let unionId = res.data.unionId;
          let openId = res.data.openId;//公总号openId
          window.location.href='https://union.chinalifeonline.com.cn/gslm/wxgslm/?channel=1&appId=wx5668b394c2053c79&funCode=""&openId='+openId+'&unionId='+unionId
        }else{
            Toast('网络请求不给力，请稍后重试');
          // Dialog.alert({ title: '温馨提示', message: "网络请求不给力，请稍后重试" })
        }
      });
    }
    if(window.location.href.indexOf("_wechat")!=-1&&window.location.href.indexOf("#/wechat")==-1){//产品授权
      console.log("进来了")
      var _this = this;
      var address = location.href;
      let appid = address.split("appid=")[1].split("&code")[0];
      let code = address.split("code=")[1].split("&state")[0]
      if(address.indexOf("quanqiuyiliao")>-1){
        let urlPro = '/pages/webview/webview'
        cloudApi.showInfo({app_id: appid, code: code}).then(function(res: any) {
        if (res.data) {
          wx.miniProgram.redirectTo({ 
            url: urlPro+'?flagOpenId='+res.data.openId
          });
        }
      });
      }else if(address.indexOf("indexPage")>-1){
        let urlPro = '/pages/index/index'
        cloudApi.showInfo({app_id: appid, code: code}).then(function(res: any) {
          if (res.data) {
            wx.miniProgram.switchTab({ 
              url: urlPro+'?flagOpenId='+res.data.openId
            });
          }
        });
      }
      // let urlPro = '/pages/webview/webview'
      // cloudApi.showInfo({app_id: appid, code: code}).then(function(res: any) {
      //   if (res.data) {
      //     wx.miniProgram.redirectTo({ 
      //       url: urlPro+'?flagOpenId='+res.data.openId
      //     });
      //   }
      // });
    }
  }
  getUnionId(){
    var _this = this;
    var address = location.href;
    let appid = address.split("appid=")[1].split("&code")[0];
    let code = address.split("code=")[1].split("&state")[0]
     cloudApi.getUnionIdAndOpenId({appid:appid, code:code}).then(function(res: any) {
        if (res.data) {
          if(res.data.unionId){
            _this.unionId = res.data.unionId;
            _this.openId = res.data.openId;//公总号openId
        }else{
            Toast('网络请求不给力，请稍后重试');
          //  Dialog.alert({ title: '温馨提示', message: "网络请求不给力，请稍后重试" })
        }
       }
      });
  }
  getUserData(){
    var address = location.href;
    let webviewUrl = address.split("state=")[1].split("_state")[0]
    if(webviewUrl=='pwebview'){
      this.urlPro ='/pages/pwebview/pwebview'
    }else if(webviewUrl=='fwebview'){
      this.urlPro ='/pages/fwebview/fwebview'
    }else if(webviewUrl=='pwebview1'){
      this.urlPro ='/pages/pwebview1/pwebview1'
    }
    console.log("zwhurlPro",this.urlPro)
    const data = {
        openid: this.openId,
        unionid: ""
    }
    let _this= this
    cloudApi.getMyBussinessCardURL(data).then(res => {
      if(res.status = 200){
        if(res.data.result.length !== 0){
          _this.mobile =res.data.result[0].mobile_Phone;
          console.log("zwhurlmobile",_this.mobile)
          _this.getToken()
        }else{
          wx.miniProgram.redirectTo({ 
            url: _this.urlPro+'?flagOpenId='+_this.openId
          });
        }
      }
    })
  }
  getToken(){
    const data = {
      mobile:this.mobile
    }
      let _this= this
      console.log("zwhurlPro",this.urlPro)
    cloudApi.getToken(data).then(res => {
      if(res.status = 200){
        if(res.data.result.body){
          console.log("zwhurlbody",res.data.result.body)
          let anxinTongData =res.data.result.body
          this.queryUserInfo(anxinTongData)
        }else{
          wx.miniProgram.redirectTo({ 
            url:_this.urlPro+'?flagOpenId='+_this.openId
         });
        }
      }
    })
  }
   queryUserInfo(anxinTongData:any){
    
    let data={
      flag: "1",
      token: anxinTongData.token,
      userId: anxinTongData.userId,
    }
    let anxinTong =JSON.stringify(anxinTongData)
    let _this= this
    cloudApi.peacequeryUserInfo(data).then(res => {
      if(res.data.result.header.statusCode == '00000000'){
        if(res.data.result.body.bindStatus == 1&&res.data.result.body.channelSource=='1'){//已绑定,销售公司
         wx.miniProgram.redirectTo({   
            url:_this.urlPro+'?flagOpenId='+_this.openId+'&anxinTongFlag=1&anxinTongData='+anxinTong
         });
        }else {
          wx.miniProgram.redirectTo({ 
            url:_this.urlPro+'?flagOpenId='+_this.openId+'&anxinTongData='+anxinTong
          });
        }
      }else{
        wx.miniProgram.redirectTo({ 
          url:_this.urlPro+'?flagOpenId='+_this.openId+'&anxinTongData='+anxinTong
        });   
      }
    })
  }
}
</script>
<style lang="less">

</style>
